package net.malkuth.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import net.malkuth.entity.LectureItem;
import net.malkuth.test.BeginClassActivity;
import net.malkuth.test.InFormationActivity;
import net.malkuth.test.R;

import java.util.List;

/**
 * Created by 一些年华 on 2016/7/8.
 */
public class LectureAdapter extends BaseAdapter {

    private LayoutInflater mInflater;

    private Context context;

    private List<LectureItem> list;


    public LectureAdapter(Context context, List<LectureItem> list) {
        this.mInflater = LayoutInflater.from(context);
        this.context = context;
        this.list = list;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHodler hodler;
        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.lecture_list_item, null);
            hodler = new ViewHodler();
            hodler.student = (Button) convertView.findViewById(R.id.btn_student);
            hodler.sex = (ImageView) convertView.findViewById(R.id.iv_sex);
            hodler.age = (TextView) convertView.findViewById(R.id.tv_age);
            hodler.level = (ImageView) convertView.findViewById(R.id.iv_level);
            hodler.bag1 = (ImageView) convertView.findViewById(R.id.iv_bag_level1);
            hodler.bag2 = (ImageView) convertView.findViewById(R.id.iv_bag_level2);
            hodler.bag3 = (ImageView) convertView.findViewById(R.id.iv_bag_level3);
            hodler.level1 = (TextView) convertView.findViewById(R.id.tv_bg_level1);
            hodler.level2 = (TextView) convertView.findViewById(R.id.tv_bg_level2);
            hodler.level3 = (TextView) convertView.findViewById(R.id.tv_bg_level3);
            hodler.info = (Button) convertView.findViewById(R.id.info);
            hodler.info1 = (Button) convertView.findViewById(R.id.info1);
            hodler.info2 = (Button) convertView.findViewById(R.id.info2);
            hodler.info3 = (Button) convertView.findViewById(R.id.info3);
            hodler.bg_sex = convertView.findViewById(R.id.bg_sex);

            hodler.select_right_image = (ImageView) convertView.findViewById(R.id.select_right_image);
            hodler.select_view = convertView.findViewById(R.id.select_view);

            hodler.layout_select = convertView.findViewById(R.id.layout_select);


            convertView.setTag(hodler);


        } else {
            hodler = (ViewHodler) convertView.getTag();
        }
        /**
         * 设置显示的内容
         */
        hodler.student.setText(list.get(position).getStudent());
        if (list.get(position).getSex() == "男生") {
            hodler.bg_sex.setBackgroundResource(R.drawable.bg_man);
            hodler.sex.setImageResource(R.drawable.ic_man);
        } else {
            hodler.bg_sex.setBackgroundResource(R.drawable.bg_woman);
            hodler.sex.setImageResource(R.drawable.ic_woman);
        }

        hodler.age.setText(list.get(position).getAge());
        if (list.get(position).getLevel() == 1) {
            hodler.level.setImageResource(R.drawable.lv1);
        } else if (list.get(position).getLevel() == 2) {
            hodler.level.setImageResource(R.drawable.lv2);
        } else {
            hodler.level.setImageResource(R.drawable.lv3);
        }
        hodler.bag1.setImageResource(R.drawable.bag1);
        hodler.bag2.setImageResource(R.drawable.bag2);
        hodler.bag3.setImageResource(R.drawable.bag3);

        if (list.get(position).getBag_level1() == 1) {
            hodler.level1.setText("初级");
        } else if (list.get(position).getBag_level2() == 2) {
            hodler.level1.setText("中级");
        } else {
            hodler.level1.setText("高级");
        }

        hodler.info.setText(list.get(position).getInfo());
        hodler.info.setBackgroundResource(R.drawable.btn_data_info_normal);
        hodler.info1.setText(list.get(position).getInfo2());
        hodler.info.setBackgroundResource(R.drawable.btn_data_info_normal);
        hodler.info2.setText(list.get(position).getInfo3());
        hodler.info.setBackgroundResource(R.drawable.bg_gray_line);
        hodler.info3.setText(list.get(position).getInfo4());
        hodler.info.setBackgroundResource(R.drawable.btn_data_info_normal);


        hodler.select_right_image.setImageResource(R.drawable.iv_select_right);
        hodler.layout_select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (hodler.select_view.getVisibility() == View.GONE) {
                    hodler.select_view.setVisibility(View.VISIBLE);
                    hodler.select_right_image.setImageResource(R.drawable.iv_select_down);
                } else if (hodler.select_view.getVisibility() == View.VISIBLE) {
                    hodler.select_view.setVisibility(View.GONE);
                    hodler.select_right_image.setImageResource(R.drawable.iv_select_right);
                }
            }
        });

        hodler.info.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, InFormationActivity.class));
            }
        });

        hodler.info3.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                context.startActivity(new Intent(context, BeginClassActivity.class));
            }
        });


        return convertView;
    }

    class ViewHodler {
        Button student;
        ImageView sex;
        TextView age;
        ImageView level;
        ImageView bag1;
        ImageView bag2;
        ImageView bag3;
        TextView level1;
        TextView level2;
        TextView level3;
        Button info;
        Button info1;
        Button info2;
        Button info3;
        View bg_sex;


        ImageView select_right_image;
        View select_view;

        View layout_select;

    }
}
